package com.victor9ac.mashup.endpoint;


import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.victor9ac.mashup.api.Commentaries;
import com.victor9ac.mashup.api.Images;
import com.victor9ac.mashup.api.travel.Travel;
import com.victor9ac.mashup.core.port.MashupJob;
import com.victor9ac.mashup.entry.port.MashupEntry;

@Component
@Path(MashupServiceEndpoint.PATH)
public class MashupServiceEndpoint implements MashupEntry {

	static final String PATH = "";
	@Autowired
	MashupJob mashupJob;


	/* (non-Javadoc)
	 * @see com.victor9ac.mashup.endpoint.MashupEntry#getIt(java.lang.String)
	 */
	@Override
	@GET
    @Path("/comments/{key}")
    @Produces(MediaType.APPLICATION_JSON)
    public Object getCommentaries(@PathParam("key") String keyword) {
    	Commentaries commentaries = mashupJob.getCommentaries(keyword);	
    	System.out.println("commentaries: "+commentaries.getCommetaries().size());
		return  commentaries;
    }
	
	/* (non-Javadoc)
	 * @see com.victor9ac.mashup.endpoint.MashupEntry#getImgs(java.lang.String)
	 */
	@Override
	@GET
    @Path("/images/{key}")
    @Produces(MediaType.APPLICATION_JSON)
    public Object getImgs(@PathParam("key") String keyword) { 	
    	Images images = mashupJob.getImages(keyword);
    	System.out.println("Images found: "+images.size());
		return  images;
    }
	
	



	/* (non-Javadoc)
	 * @see com.victor9ac.mashup.endpoint.MashupEntry#getTravelFromTo(java.lang.String, java.lang.String)
	 */
	@Override
	@GET
    @Path("/opentravel/{from}/{to}")
    @Produces(MediaType.APPLICATION_JSON)
    public Object getTravelFromTo(@PathParam("from") String from, @PathParam("to")String to) {
		

		Travel travel = mashupJob.getOpenTravel(from, to);
		checkTravelResult(travel);

		return travel;
			
	}
	
	/* (non-Javadoc)
	 * @see com.victor9ac.mashup.endpoint.MashupEntry#getTravelDefault(java.lang.String)
	 */
	@Override
	@GET
    @Path("/travel/{to}")
    @Produces(MediaType.APPLICATION_JSON)
    public Object getTravelDefault(@PathParam("to") String keyword) { 	
    	
		Travel travel = mashupJob.getTravelByDestination(keyword);
	
		checkTravelResult(travel);
		
		return travel;
    }

	/* (non-Javadoc)
	 * @see com.victor9ac.mashup.endpoint.MashupEntry#getTravel(java.lang.String, java.lang.String)
	 */
	@Override
	@GET
    @Path("/travel/{from}/{to}")
    @Produces(MediaType.APPLICATION_JSON)
    public Object getTravel(@PathParam("from") String from, @PathParam("to")String to) {
		

		Travel travel = mashupJob.getTravel(from, to);
		checkTravelResult(travel);

		return travel;
		
		
		
	}

	
	private void checkTravelResult(Travel travel) {
		if (travel != null){
			System.out.println("Travel obtained from:"+travel.getFrom()+" to: " +travel.getTo());
		}
	}


	




}
